<?php 
define("CCTRL", "orders");
define("CCTRLNAME", "order");
session_start();
class orders extends CI_Controller  {

        /**
        * Index Page for this controller.
        *
        * Maps to the following URL
        * 		http://example.com/index.php/welcome
        *	- or -  
        * 		http://example.com/index.php/welcome/index
        *	- or -
        * Since this controller is set as the default controller in 
        * config/routes.php, it's displayed at http://example.com/
        *
        * So any other public methods not prefixed with an underscore will
        * map to /index.php/welcome/<method_name>
        * @see http://codeigniter.com/user_guide/general/urls.html
        */
       private $_configs = null;
       function __construct()
        {
            parent::__construct();
            $this->load->helper('url');
            $this->load->library('smarty3','','smarty');
            $this->load->model('admin-planners/order_model','my_model');
            $this->load->model('admin-planners/setting_model','setting_model');
            include APPPATH . 'libraries/defu.php';
            if(!isset($_SESSION["ADP"][CCTRL]["display"]))$_SESSION["ADP"][CCTRL]["display"]=1;
            $this->InitSetting();
        }
        function InitSetting(){
            $colModel=array(
                array(  "display"       =>"Order ID"            ,"name"=>"OrderID"          ,"width"=> 80       ,"sortable"=>true       ,"align"=>"center"      ,"hide"=>false      ,"filter"=>true),
                array(  "display"       =>"Type"                ,"name"=>"Paypal"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"      ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Customer ID"         ,"name"=>"CustomerID"       ,"width"=> 80       ,"sortable"=>true       ,"align"=>"center"      ,"hide"=>false      ,"filter"=>true ),
                array(  "display"       =>"Customer Name"       ,"name"=>"CustomerName"     ,"width"=>180       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>true ),
                array(  "display"       =>"Order Date"          ,"name"=>"OrderDate"        ,"width"=>100       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship Name"           ,"name"=>"ShipName"         ,"width"=>120       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship Address"        ,"name"=>"ShipAddress"      ,"width"=>120       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship City"           ,"name"=>"ShipCity"         ,"width"=>120       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship Region"         ,"name"=>"ShipRegion"       ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship Postal Code"    ,"name"=>"ShipPostalCode"   ,"width"=> 80       ,"sortable"=>true       ,"align"=>"right"       ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Ship Country"        ,"name"=>"ShipCountry"      ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false ),
                array(  "display"       =>"Monney"              ,"name"=>"Monney"           ,"width"=> 60       ,"sortable"=>true       ,"align"=>"right"       ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Status"              ,"name"=>"Status"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Insert"              ,"name"=>"Insert"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>false      ,"filter"=>false),
                array(  "display"       =>"Update"              ,"name"=>"Update"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false),
                array(  "display"       =>"Delete"              ,"name"=>"Delete"           ,"width"=> 80       ,"sortable"=>true       ,"align"=>"left"        ,"hide"=>true       ,"filter"=>false)
            );
            if(isset($_SESSION["ADP"]["USER"])){
                if(isset($_SESSION["ADP"][CCTRL]["colModel"])){
                    $colModel= $_SESSION["ADP"][CCTRL]["colModel"];
                }else{
                    $_SESSION["ADP"][CCTRL]["colModel"]=$colModel;
                }
            }
        }
        public function index()
	{
            
            if(isset($_SESSION["ADP"]["Filter_".CCTRL]))unset($_SESSION["ADP"]["Filter_".CCTRL]);
            if(!isset($_SESSION["ADP"]["USER"])){
                $this->smarty->display("admin-planners/01_login");
                return;
            }
            $Data["flexigrid_settings"]["colModel"]=$_SESSION["ADP"][CCTRL]["colModel"];
            $Data["flexigrid_settings"]["display"]=$_SESSION["ADP"][CCTRL]["display"];
            $Data["flexigrid_settings"]["filterModel"]=filterModel($_SESSION["ADP"][CCTRL]["colModel"]);
            $this->smarty
                    ->assign('_SESSION', $_SESSION)
                    ->assign('Data', $Data)
                    ->display("admin-planners/".CCTRLNAME."/".CCTRLNAME);
	}
        
        public function ChangeColumnDisplay(){
            for($i=0;$i<count($_SESSION["ADP"][CCTRL]["colModel"]);$i++){
                if($_SESSION["ADP"][CCTRL]["colModel"][$i]["name"]==$_POST["col"]){
                    $_SESSION["ADP"][CCTRL]["colModel"][$i]["hide"]=($_POST["hide"]==1?true:false);
                    break;
                }
            }
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function ChangeDeleteDisplay(){
            $_SESSION["ADP"][CCTRL]["display"]=$_POST["showDelete"];
            $code=1;
            $msg="Data display have been change.";
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function Edit(){
            $Data=null;
            if(isset($_POST["ID"])){
                $pr=$this->my_model->get($_POST["ID"]);
                if(count($pr)>0)$Data["OBJ"]=$pr;
            }
            $this->smarty->assign('_SESSION', $_SESSION);
            $this->smarty->assign('Data', $Data);
            $this->smarty->display("admin-planners/order/02_edit");
        }
        public function Delete(){
            if(isset($_POST["ID"])){
                $ip=  getIP();
                if($this->my_model->delete($_POST["ID"])){
                    $code=1;
                    $msg="Item đã được xóa.";
                }else{
                    $code=-1;
                    $msg="Lỗi. Chưa xóa được item.";
                }
            }else{  
                $code=-1;
                $msg="Không xác định được dữ liệu cần xóa.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function Restore(){
            if(isset($_POST["ID"])){
                $ip=  getIP();
                if($this->my_model->restore($_POST["ID"])){
                    $code=1;
                    $msg="Item đã được khôi phục.";
                }else{
                    $code=-1;
                    $msg="Lỗi. Chưa khôi phục được item.";
                }
            }else{  
                $code=-1;
                $msg="Không xác định được dữ liệu cần khôi phục.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        public function ChangeStatus(){
            
            if(isset($_POST["Status"]) && isset($_POST["ID"])){
                $Params=array(
                    "Status"=>$_POST["Status"]
                );
                //$ip=  getIP();
                //$VideoParams["Log"]="    IP : $ip \n    Action : Change Status \n    RowID : ".$_POST["VideoID"]."\n    New Status : ".$_POST["Status"];
                if($this->my_model->update($_POST["ID"],$Params)){
                    //$this->log_model->insert(array("Table"=>"Video","RowID"=>$_POST["VideoID"],"Action"=>"Change Status","Log"=>$VideoParams["Log"]));
                    $code=1;
                    $msg="Trạng thái Item đã được thay đổi.";
                }else{
                    $code=-1;
                    $msg="Lỗi. Không thể thay đổi trạng thái Item.";
                }
            }else{  
                $code=-1;
                $msg="Không xác định được dữ liệu.";
            }
            echo json_encode(array("code"=>$code,"msg"=>$msg));
        }
        function FlexiGridData(){
            $data=$this->my_model->FlexiGridData();
            header("Content-type: application/json");
            $jsonData = array('page'=>$data["page"],'total'=>$data["total_rows"],'rows'=>array());
            foreach($data["rows"] AS $row){
                    //If cell's elements have named keys, they must match column names
                    //Only cell's with named keys and matching columns are order independent.
                    
                    $cell=array();
                    foreach ($_SESSION["ADP"][CCTRL]["colModel"] as $col){
                        $c=$col["name"];
                        $cell[$col["name"]]=$row->$c;
                    }
                    $cell['CustomerName']=htmlentities_UTF8($row->CustomerName);
                    $id="OrderID";
                    $entry = array('id'=>$row->$id,
                            'cell'=>$cell
                    );
                    $jsonData['rows'][] = $entry;
            }
            echo json_encode($jsonData);
        }
        function Filter(){
            echo json_encode(array("code"=>1,"msg"=>"Filter susess."));
        }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */